package com.lala.consumer.controller;

import com.alibaba.fastjson.JSONArray;
import com.lala.commom.pojo.User;
import com.lala.consumer.service.UserService;
import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;
    public User yanzheng(HttpServletRequest request){
        User user=null;
        Cookie[] cookies =  request.getCookies();//获取保存在request的所有cookie
        if(cookies != null){//判断cookies数组是否为空
            for(Cookie cookie : cookies){
                if(cookie.getName().equals("user")){//通过for循环找到想要获取的值
                    Map<String ,Object> map=(Map<String ,Object>) redisTemplate.opsForValue().get(cookie.getValue());
                    if(map!=null){
                        user=(User)map.get("user");
                    }
                }
            }
        }

        return user;
    }

    //登录
    @RequestMapping("/login")
    public Object login(String loginName, String password, HttpServletResponse response){
        User user=userService.selectLoginname(loginName);
        if (user==null){
            return "false";
        }else if (user.getPassword().equals(password)){
            Map<String,Object>map=new HashMap<>();
            map.put("user",user);
            Date date=new Date();
            map.put("date",date.getTime());
            String id=UUID.randomUUID().toString();
            Cookie cookie=new Cookie("user", id);
            cookie.setMaxAge(60*30);
            response.addCookie(cookie);
            redisTemplate.opsForValue().set(id,map,30, TimeUnit.MINUTES);
            return JSONArray.toJSON(user);
        }else {
            return "mmcw";
        }
    }

    //前台发Ajax验证
    @RequestMapping("/user")
    private Object user(HttpServletRequest request){
        User user=null;
        Date date=new Date();
        long l=date.getTime();
        Cookie[] cookies =  request.getCookies();//获取保存在request的所有cookie
        if(cookies != null){//判断cookies数组是否为空
            for(Cookie cookie : cookies){
                if(cookie.getName().equals("user")){//通过for循环找到想要获取的值
                    Map<String ,Object> map=(Map<String ,Object>) redisTemplate.opsForValue().get(cookie.getValue());
                    if(map!=null){
                        user=(User)map.get("user");
                        long minutes = ((l-(long)map.get("date")) / 1000) / 60;
                        if (minutes>=25){
                            Map<String,Object>param=new HashMap<>();
                            param.put("user",user);
                            Date d=new Date();
                            param.put("date",d.getTime());
                            redisTemplate.opsForValue().set(cookie.getValue(),param);
                        }
                    }
                }
            }
        }

        return JSONArray.toJSON(user);
    }

    //注销
    @RequestMapping("/zhuxiao")
    private Object zhuxiao (HttpServletRequest request){
        Cookie[] cookies =  request.getCookies();//获取保存在request的所有cookie
        if(cookies != null){//判断cookies数组是否为空
            for(Cookie cookie : cookies){
                if(cookie.getName().equals("user")){//通过for循环找到想要获取的值
                    redisTemplate.delete(cookie.getValue());
                    return "true";
                }
            }
        }
      return "false";
    }

    @RequestMapping("/zhuce")
    public  Object zhuce (@RequestParam Map<String ,Object> map) throws ParseException {
        User user=new User();
        SimpleDateFormat sim= new SimpleDateFormat("yyyy-MM-dd");
        try {
            user.setBirthday(sim.parse(map.get("birthday").toString()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        user.setEmail(map.get("email").toString());
        user.setGender(Integer.parseInt(map.get("gender").toString()));
        user.setPhonenumber(map.get("phoneNumber").toString());
        user.setName(map.get("name").toString());
        user.setLoginname(map.get("loginName").toString());
        user.setPassword(map.get("password").toString());
        user.setPostid(5);
        user.setGrade(0);
        user.setMoney(0);
        user.setPhone(null);
        user.setPhoto(null);
        int i=userService.insert(user);
        if (i>0){
            return "true";
        }else {
            return "false";
        }
    }
}
